{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件夹 '0109' 已存在。\n",
      "文件夹 '四望镇' 已创建。\n",
      "构筑物数量：'1'\n",
      "要修改数量：'27'\n",
      "未修改数量：'2307'\n",
      "正在移除图层：四望镇_中心镇区\n",
      "图层移除完成，并已保存更改\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from datetime import datetime\n",
    "import arcpy\n",
    "from pypinyin import lazy_pinyin\n",
    "\n",
    "def check_and_create_folder(folder_path, folder_name):\n",
    "    full_path = os.path.join(folder_path, folder_name)\n",
    "\n",
    "    if not os.path.exists(full_path):\n",
    "        os.makedirs(full_path)\n",
    "        print(f\"文件夹 '{folder_name}' 已创建。\")\n",
    "    else:\n",
    "        print(f\"文件夹 '{folder_name}' 已存在。\")\n",
    "\n",
    "    return full_path\n",
    "\n",
    "def create_town(base_path, townName):\n",
    "    # 检查并创建镇文件夹\n",
    "    town_folder = check_and_create_folder(base_path, townName)\n",
    "\n",
    "    return town_folder\n",
    "\n",
    "def create_wh(base_path, whiteModelName, wmName):\n",
    "    # 检查并创建白膜文件夹\n",
    "    whiteModel_folder = check_and_create_folder(base_path, whiteModelName)\n",
    "    # 检查并创建白膜名称文件夹\n",
    "    wm_folder = check_and_create_folder(whiteModel_folder, wmName)\n",
    "\n",
    "    return whiteModel_folder, wm_folder\n",
    "\n",
    "isagain = False\n",
    "version = \"02\"\n",
    "path_to_check = r\"E:\\MyDocument\\ArcGis\\DT\\基地面修改\"\n",
    "folder_to_find = datetime.now().strftime(\"%m%d\")\n",
    "\n",
    "base_path = check_and_create_folder(path_to_check, folder_to_find)\n",
    "townName = \"四望镇\"\n",
    "town_folder = create_town(base_path, townName)\n",
    "\n",
    "# 获取当前工程\n",
    "aprx = arcpy.mp.ArcGISProject(\"CURRENT\")\n",
    "\n",
    "# 获取活动的地图或场景\n",
    "active_map_or_scene = aprx.activeMap\n",
    "# 查找对应名称的图层\n",
    "target_layer_name = \"四望镇_中心镇区\"\n",
    "\n",
    "target_layer = None\n",
    "for lyr in active_map_or_scene.listLayers():\n",
    "    if lyr.name == target_layer_name:\n",
    "        target_layer = lyr\n",
    "        break\n",
    "\n",
    "if target_layer is None:\n",
    "    print(\"图层未找到。\")\n",
    "    exit\n",
    "\n",
    "# 连接参数\n",
    "sheet_name = \"Sheet1$\"\n",
    "in_field = \"uuid\"\n",
    "join_field = \"MOID\"\n",
    "\n",
    "# 添加连接\n",
    "veg_joined_table = arcpy.management.AddJoin(\n",
    "    target_layer_name,\n",
    "    in_field,\n",
    "    sheet_name,\n",
    "    join_field)\n",
    "\n",
    "# 拆分文件 要修改、不修改、构筑物\n",
    "modify_name = townName + \"_要修改\" + (version if isagain else '')\n",
    "modify_path = os.path.join(town_folder, modify_name)\n",
    "nomodify_name = townName + \"_不修改\" + (version if isagain else '')\n",
    "nomodify_path = os.path.join(town_folder, nomodify_name)\n",
    "delete_name = townName + \"_构筑物\" + (version if isagain else '')\n",
    "delete_path = os.path.join(town_folder, delete_name)\n",
    "wh_name = townName + \"_中心镇区\" + (version if isagain else '')\n",
    "wh_path = os.path.join(town_folder, wh_name)\n",
    "\n",
    "expriession = \"Sheet1$.WTBH = '删除'\"\n",
    "out_layer_or_view, countStr = arcpy.management.SelectLayerByAttribute(\n",
    "    target_layer,\n",
    "    \"NEW_SELECTION\",\n",
    "    expriession)\n",
    "print(f\"构筑物数量：'{countStr}'\")\n",
    "\n",
    "count = int(countStr)\n",
    "if count > 0:\n",
    "    arcpy.conversion.ExportFeatures(out_layer_or_view, delete_path)\n",
    "\n",
    "expression = \"Sheet1$.WTBH = '拆分' Or Sheet1$.WTBH = '合并' Or Sheet1$.WTBH = '其他'\"\n",
    "out_layer_or_view, countStr = arcpy.management.SelectLayerByAttribute(\n",
    "    target_layer, \n",
    "    \"NEW_SELECTION\", \n",
    "    expression)\n",
    "print(f\"要修改数量：'{countStr}'\")       \n",
    "\n",
    "modify_count = int(countStr)\n",
    "if modify_count > 0:\n",
    "    arcpy.conversion.ExportFeatures(out_layer_or_view, modify_path)\n",
    "\n",
    "expression = \"Sheet1$.MOID IS NULL\"\n",
    "out_layer_or_view, countStr = arcpy.management.SelectLayerByAttribute(\n",
    "    target_layer,\n",
    "    \"NEW_SELECTION\",\n",
    "    expression)\n",
    "print(f\"未修改数量：'{countStr}'\")\n",
    "\n",
    "count = int(countStr)\n",
    "if count > 0:\n",
    "    if modify_count == 0:\n",
    "        arcpy.conversion.ExportFeatures(out_layer_or_view, wh_path)\n",
    "    else:\n",
    "        arcpy.conversion.ExportFeatures(out_layer_or_view, nomodify_path)\n",
    "\n",
    "# 移除连接\n",
    "arcpy.management.RemoveJoin(target_layer_name)\n",
    "\n",
    "# 移除图层\n",
    "if target_layer:\n",
    "    print(f\"正在移除图层：{target_layer.name}\")\n",
    "    active_map_or_scene.removeLayer(target_layer)\n",
    "    # 保存更改到工程文件\n",
    "    aprx.save()\n",
    "    print(\"图层移除完成，并已保存更改\")\n",
    "else:\n",
    "    print(f\"未找到名为 '{target_layer_name}' 的图层\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ArcGISPro",
   "language": "Python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".py",
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
